package rickyGame.config
{
	import flash.utils.Dictionary;
	import flash.utils.describeType;
	
	import rickyGame.config.Ability.Ability;
	import rickyGame.config.Ability.AbilityData;
	import rickyGame.config.unit.BuildingUnit;
	import rickyGame.config.unit.GoodsUnit;
	import rickyGame.config.unit.HeroUnit;
	import rickyGame.config.unit.Unit;


	/**
	 * 配置文件帮助类
	 */
	public class GameUnitHelper
	{
		public function GameUnitHelper()
		{
			//
		}
		
		//*************************** 常量 ******************************
		
		/**
		 * 英雄配置key
		 */
		public static const  KeyHeroUnit:String="KeyHeroUnit";
		
		/**
		 * 技能配置文件key
		 */
		public static const KeyAbility:String="KeyAbility";
		
		/**
		 * 技能数据配置文件key 
		 */
		public static const KeyAbilityData:String="KeyAbilityData";
		
		/**
		 * 建筑数据配置文件key 
		 */
		public static const KeyBuildingUnit:String="KeyBuildingUnit";
		
		/**
		 * 物品数据配置文件key 
		 */
		public static const KeyGoodsUnit:String="KeyGoodsUnit";
		
		
		/**
		 * 初始化配置文件
		 */
		public static function init():void
		{
			//初始化英雄单位
			var data:*=new TxtHeroUnit().toString();
			var arr:Dictionary=decompressionDic(data,HeroUnit,"unitID");
			cache[KeyHeroUnit]=arr;
			
			//初始化技能数据
			data=new TxtAblilityData().toString();
			arr=decompressionDic(data,AbilityData,"abDataID");
			cache[KeyAbilityData]=arr;
			
			//初始化技能
			data=new TxtAblility().toString();
			arr=decompressionDic(data,Ability,"abilityID");
			var alldata:Dictionary=cache[KeyAbilityData];
			var itemdata:Array,item:AbilityData;
			for each(var ab:Ability in arr)
			{
				itemdata=ab.data.split(","); 
				ab.dataArr=[];
				for each(var abName:String in itemdata)
				{
					item=alldata[abName];
					if(item)
						ab.dataArr.push(item);
				}
			}
			cache[KeyAbility]=arr;
			
			//初始建筑物数据
			data=new TxtBuildingUnit().toString();
			arr=decompressionDic(data,BuildingUnit,"unitID");
			cache[KeyBuildingUnit]=arr;
			
			//初始物品数据
			data=new TxtGoodsUnit().toString();
			arr=decompressionDic(data,GoodsUnit,"unitID");
			cache[KeyGoodsUnit]=arr;
			
			arr=null;
		}
		
		/**
		 * 得到配置数据
		 */
		public static function getConfigByKey(key:String):Array
		{
			var r:Dictionary=cache[key];
			if(r)
			{
				var re:Array=[];
				
				for each(var item:* in r)
					re.push(item);
					
				return re;
			}
			else
				return null;
		}
		
		/**
		 * 得到配置数据
		 */
		public static function getDicConfigByKey(key:String):Dictionary
		{
			var r:Dictionary=cache[key];
			if(r)
				return r;
			else
				return null;
		}
		
		/**
		 * 得到单位配置
		 */
		public static function getUnitConfig(unitId:String):Unit
		{
			var unit:Unit=null;
			unit=getHeroConfig(unitId);
			if(!unit)
				unit=getBuildingConfig(unitId);
			if(!unit)
				unit=getGoodsConfig(unitId);
			return unit;
		}
		
		/**
		 * 得到英雄配置数据
		 */
		public static function getHeroConfig(unitId:String):HeroUnit
		{
			var arr:Dictionary= cache[KeyHeroUnit];
			if(arr)
				return arr[unitId];
			return null;
		}
		
		/**
		 * 得到技能配置
		 */
		public static function getAbilityConfig(abilityID:String):Ability
		{
			var re:Dictionary= cache[KeyAbility];
			if(re)
				return re[abilityID];
			return null;
		}
		
		/**
		 * 得到建筑配置
		 */
		public static function getBuildingConfig(unitId:String):BuildingUnit
		{
			var re:Dictionary= cache[KeyBuildingUnit];
			if(re)
				return re[unitId];
			return null;
		}
		
		/**
		 * 得到物品配置
		 */
		public static function getGoodsConfig(unitID:String):GoodsUnit
		{
			var re:Dictionary= cache[KeyGoodsUnit];
			if(re)
				return re[unitID];
			return null;
		}
		
		private static var cache:Dictionary=new Dictionary();
		
		//********************* 文本配置文件 ***********************
		[Embed(source="rickyGame/config/unit/HeroUnit.txt",mimeType="application/octet-stream")]
		/**
		 * 英雄配置文件
		 */
		public static var TxtHeroUnit:Class;
		
		[Embed(source="rickyGame/config/Ability/Ability.txt",mimeType="application/octet-stream")]
		/**
		 * 技能配置文件
		 */
		public static var TxtAblility:Class;
		
		[Embed(source="rickyGame/config/Ability/AbilityData.txt",mimeType="application/octet-stream")]
		/**
		 * 技能数据配置文件
		 */
		public static var TxtAblilityData:Class;
		
		[Embed(source="rickyGame/config/unit/BuildingUnit.txt",mimeType="application/octet-stream")]
		/**
		 * 建筑配置文件
		 */
		public static var TxtBuildingUnit:Class;
		
		[Embed(source="rickyGame/config/unit/GoodsUnit.txt",mimeType="application/octet-stream")]
		/**
		 * 物品配置文件
		 */
		public static var TxtGoodsUnit:Class;
		
		/**
		 * 解压文本为对象
		 */
		public static function decompression(data:*,className:Class=null):Array
		{
			var g:Object;//对像
			var dis:XMLList=null;
			if(className!=null)
				dis=describeType(new className).accessor;
			//生成对像
			var rows:Array=data.split("\r\n");//所有行
			var property:Array=rows[1].split("\t");//第二行
			var columns:Array;//列
			var _guide:Array=new Array();
			var p:String;//属性
			var c:String;//值
			var t:String;//类型
			var accessor:*;//访问器
			var rowStri:String;

			for(var i:int=2;i<rows.length;i++)
			{
				rowStri=rows[i];
				if(rowStri.indexOf("//")!=-1)
					continue;
				
				columns=rowStri.split("\t");
				if(className==null)
					g=new Object();
				else
					g=new className();
				
				for (var j:int=0;j<property.length;j++)
				{
					if(j<columns.length)
						c=columns[j];
					else
						c="";
					p=property[j];
					
					if(dis!=null)//如果有描述
					{
						accessor=dis.(@name==p);
						if(accessor)
						{
							t=accessor.@type;//类型
							if(t=="*")
								t="Object";
							g[p]=flash.utils.getDefinitionByName(t)(c);
						}
						else
							g[p]=c;
					}
					else
						g[p]=c;
					
				}	
				_guide.push(g);
			}
			
			return _guide;
		}
		
		
		/**
		 * 解压文本为对象返回Dictionary对象
		 */
		public static function decompressionDic(data:*,className:Class=null,key:String=""):Dictionary
		{
			var g:Object;//对像
			var dis:XMLList=null;
			if(className!=null)
				dis=describeType(new className).accessor;
			//生成对像
			var rows:Array=data.split("\r\n");//所有行
			var property:Array=rows[1].split("\t");//第二行
			var columns:Array;//列
			var dic:Dictionary=new Dictionary();
			var p:String;//属性
			var c:String;//值
			var t:String;//类型
			var accessor:*;//访问器
			var rowStri:String;
			
			for(var i:int=2;i<rows.length;i++)
			{
				rowStri=rows[i];
				if(rowStri.indexOf("//")!=-1)
					continue;
				
				columns=rowStri.split("\t");
				if(className==null)
					g=new Object();
				else
					g=new className();
				
				for (var j:int=0;j<property.length;j++)
				{
					if(j<columns.length)
						c=columns[j];
					else
						c="";
					p=property[j];
					
					if(dis!=null)//如果有描述
					{
						accessor=dis.(@name==p);
						if(accessor)
						{
							t=accessor.@type;//类型
							if(t=="*")
								t="Object";
							
//							if(t=="Boolean")
//							{
//								g[p]=Boolean(c);
//							}
//							else
								g[p]=flash.utils.getDefinitionByName(t)(c);
						}
						else
							g[p]=c;
					}
					else
						g[p]=c;
					
				}	
				dic[g[key]]=g;
			}
			
			return dic;
		}
	}
}